package demolist;

public class MySingleList {
    static class ListNode {//内部类，可由主类名直接调用，不用实例化
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(11);
        ListNode node2 = new ListNode(34);
        ListNode node3 = new ListNode(44);
        ListNode node4 = new ListNode(23);
        ListNode node5 = new ListNode(88);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

    public void show() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //是否包含key
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //在任意位置插入数据
    public void addIndex(int index, int val) {
        if (index < 0 || index > size()) {
            return;
        }
        if (index == 0) {
            addFirst(val);
            return;
        }
        if (index == size()) {
            addLast(val);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(val);
        node.next = cur.next;
        cur.next = node;
    }

    /**
     * 找到index位置的前一个结点
     */
    private ListNode findIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    //删除第一个出现的关键字key
    public void remove(int key) {
        if (head == null) return;
        if (head.val == key) {
            head = head.next;
            return;
        }

        ListNode cur = findKey(key);
        if (cur == null) return;

        ListNode del = cur.next;
        cur.next = del.next;
    }

    /**
     * 找到要删除的关键字的前一个节点
     */
    private ListNode findKey(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        //没有找到
        return null;
    }

    public void removeAllKey(int key) {
        if (head == null) return;
//        while (head.val == key) {
//            head = head.next;
//        }
        ListNode prev = head;//prev是头节点
        ListNode cur = head.next;//cur是头节点的下一个节点
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;//prev节点指向cur的下一个节点
                cur = cur.next;//cur变成cur的下一个节点
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == key) {
            head = head.next;
        }
    }

    //清空链表
    public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }

    //链表反转
    public ListNode reverseList() {
        if (head == null) {
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;
    }

    //从指定位置开始进行打印
    public void show(ListNode newHead) {
        ListNode cur = newHead;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //找到链表的中间节点
    public ListNode findMiddleNode1() {//1.需要遍历链表两遍
        if (head == null) {
            return null;
        }
        int len = size();
        int count = 0;
        ListNode cur = head;
        while (count != len / 2) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    /**
     * 原理：fast的速度是slow速度的两倍，当fast走完全程时，slow刚好走到路程的中间位置
     * @return
     */
    public ListNode findMiddleNode2() {//2.只用遍历一遍
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {//注意：判断条件位置不能换，要先判断大范围再判断小范围
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //找出链表中倒数第K个节点，并返回该节点的值
    public int kthToLast(int k) {
        if (k <= 0 || k >size()) {
            return -1;
        }
        int len = size();
        int count = len - k;
        ListNode cur = head;
        while (count != 0) {
            cur = cur.next;
            count--;
        }
        return cur.val;
    }

    public int kthToLast2(int k) {
        if (k <= 0) {
            return -1;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = k - 1;
        while (count != 0) {
            fast = fast.next;
            if (fast == null) {//巧妙之处，数据结构与算法
                return -1;
            }
            count--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.val;
    }

    /**
     * 给x，将链表中小于x的节点放在链表的前面，不改变顺序(链表分割)
     * @param x
     * @return
     */
    public ListNode partition(int x) {
        ListNode cur = head;
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        while(cur != null) {
            if(cur.val < x) {//放在链表的前面
                if(bs == null) {//第一次插入
                    bs = be = cur;
                } else {//不是第一次插入
                    be.next = cur;
                    be = be.next;
                }
            } else {//放在链表的后面
                if(as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if (bs == null) {//当没有节点的val小于x时
            return as;
        }
        be.next = as;
        if (as != null) {
            ae.next = null;//以防找不到“尾巴”
        }
        return bs;
    }

    public boolean chkPalindrome() {
        if (head == null) {
            return false;
        }
        //1.找中间节点
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2.反转
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;//slow指向cur
            cur = curN;//cur指向curN
        }
        //3.开始判断
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    /**
     * 判断链表是否有环
     * @param head
     * @return
     */
    public static boolean hasCycle(ListNode head) {
        if (head == null) return false;
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    /**
     * 创建带环链表
     */
    public void createLoop() {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = head.next.next.next;
    }

    /**
     * 求链表的环的入口点
     */
    public ListNode detecCycle(ListNode head) {
        if (head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}

